package com.ossean.match.utils;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

public class Normalizer {

	public static String removeVersion(String str){
		
		//现在对于2.45还不能处理，也就是点后边两个连续的后缀
		return str.replaceAll("\\-*\\d+(\\.[\\d|x|X])*$","");
	}
	
	//对字符串进行规整，去除版本号后缀和连接符等
	public static String normalize(String str) {
		//目前可能会删除掉一些确实以数字结尾的项目；在停用词里添加‘-’
		return str.replaceAll("[\\-]+"," ").replaceAll("'s"," ").replaceAll("[^0-9a-zA-Z\u4E00-\u9FA5]"," ").toLowerCase().trim();
	}
	
	/**
	 * 清除掉所有特殊字符
	 * 
	 * @param str
	 * @return
	 * @throws PatternSyntaxException
	 */
//	public static String StringFilter(String str) throws PatternSyntaxException {
//		if (str == null) {
//			return "";
//		}
//		String regEx = "[`~!@#$%^&*()+-_=|{}':;',\\[\\].<>/?~！@#\"￥%……&（）——+|{}【】‘；：”“’。，、？]";
//		Pattern p = Pattern.compile(regEx);
//		Matcher m = p.matcher(str);
//		return m.replaceAll(" ").trim();
//	}
	
	/**
	 * IK分词器分词
	 * @param text
	 * @return
	 */
	public static List<String> getList(String text) {
		List<String> tempArrayList = new ArrayList<String>();
		Reader reader = new StringReader(text);
		IKSegmenter iks = new IKSegmenter(reader, true);
		Lexeme term = null;
		try {
			while ((term = iks.next()) != null) {
				tempArrayList.add(term.getLexemeText().toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return tempArrayList;
	}

	public static void main(String []args) {
//		String content = "大声道Corvette123 *&%9&^&%&^%(*_asd's_12 L'Aur--ore, 1766-1775";
//		int a = 1 + 11000/11000;
//		String targetTableName = "relative_memo_to_open_source_projects_" + a;
//		//System.out.println(StringFilter(content));
//        String result = content.replaceAll("[^0-9a-zA-Z]"," ");
//        System.out.println(result);
//        System.out.println(normalize(content));
//        System.out.println(content.lastIndexOf(" "));
//        //System.out.println(StringFilter(content));
		String a = "'One' PHP cache";
		System.out.println(getList(a));
	}
	/**
	 * if a string only contains digital
	 * 
	 * @param checkStr
	 * @return
	 */
	public static boolean isAllNumber(String checkStr) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(checkStr);
		return isNum.matches();
	}

	// 标签分离函数
	public static List<String> tagsSegmentation(String tags) {
		List<String> tag = new ArrayList<String>();

		if (tags != null) {
			String regex = "<[^<>]*>";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(tags);

			while (matcher.find()) {
				String t = matcher.group();
				t = t.substring(1, t.length() - 1);

				tag.add(t);
			}
		}
		return tag;
	}
	
}
